/* =============================================================================
 * BeeWorks MoneyPile - Domain Model
 *
 * 
 * =============================================================================
 *
 * Developers: Jurgen Lust
 *
 * 
 * (C) Copyright 2009, BeeWorks BVBA.
 *
 */

package be.beeworks.moneypile.model;

import be.beeworks.moneypile.model.enums.DocumentType;
import be.beeworks.moneypile.model.enums.Quarter;

import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

import java.io.Serializable;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


/**
 * A document that has some meaning to the company, for example an invoice,
 * a contract, an expense, ...
 *
 * @author jlust
 * @author $LastChangedBy: Jurgen.Lust $
 *
 * @version $LastChangedRevision: 15 $
 */
@Entity
@Table(name = "ABSTRACT_DOCUMENTS")
@Indexed
@Inheritance(strategy = InheritanceType.JOINED)
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class AbstractDocument
        implements Serializable {
    //~ Static fields/initializers ---------------------------------------------

    private static final long serialVersionUID = 9209254140727908301L;

    //~ Instance fields --------------------------------------------------------

    @NotNull
    @Temporal(TemporalType.DATE)
    private Date creationDate;
    @NotNull
    private Integer year;
    @Version
    @XmlTransient
    private Long version;
    @NotNull
    @Enumerated(EnumType.ORDINAL)
    private Quarter quarter;
    @NotNull
    @Size(min = 1, max = 100)
    @Field(index = Index.TOKENIZED, store = Store.NO)
    private String description;
    @Size(max = 100)
    @Field(index = Index.TOKENIZED, store = Store.NO)
    private String filename;
    @Id
    @XmlID
    @DocumentId
    @Size(min = 32, max = 32)
    private String id;
    @Size(max = 2000)
    @Field(index = Index.TOKENIZED, store = Store.NO)
    private String notes;

    //~ Constructors -----------------------------------------------------------

    public AbstractDocument() {
        super();
        this.id = UUID.randomUUID().toString().replaceAll("-", "");

        Calendar today = GregorianCalendar.getInstance();
        this.creationDate = today.getTime();
        this.year = today.get(Calendar.YEAR);
        this.quarter = Quarter.values()[today.get(Calendar.MONTH / 3)];
    }

    //~ Methods ----------------------------------------------------------------

    public String getId() {
        return id;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public Quarter getQuarter() {
        return quarter;
    }

    public void setQuarter(Quarter quarter) {
        this.quarter = quarter;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public abstract DocumentType getType();

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public Long getVersion() {
        return version;
    }
}
